//
//  PostCarTableViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/19.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "PostCarTableViewController.h"
#import "PickerTableViewCell.h"
#import "selectView.h"
#import "PikerImageTableViewCell.h"
#import "CarpubMode.h"
#import "Pubmode.h"
#import "IWUploadParam.h"
#import "EidtTableViewCell.h"
#import "PublishTopTableViewCell.h"
#import "RadioTableViewCell.h"
#import "NoramlCellMode.h"
#import "PickViewPhotoTableViewCell.h"
#import "PickerTableViewController.h"
#import  <AssetsLibrary/AssetsLibrary.h>
#import <QBImagePickerController/QBImagePickerController.h>
#import "CarbrandModel.h"
#import "CarprogChildmode.h"
#import "ColorPickViewController.h"
#import "FindCarTableViewController.h"
#import "CarsrouceTypePickerController.h"
#import "CarDetail.h"
#import "ListPikerController.h"
#import "Position.h"
#import "Area.h"
#import "Process.h"
#import "JianjieViewController.h"
#import "CarPickModel.h"
#import "CarSourceType.h"
#import "Puser.h"
#import "PostSUSViewController.h"
#import "CarDetailTableViewCell.h"
#import "PostResdemode.h"
#import "IndexCarModel.h"
#import "CarDetailmodel.h"
#import "Puser.h"
#import <IQKeyboardManager.h>
#import <IQKeyboardReturnKeyHandler.h>


@interface PostCarTableViewController ()<SelectCellDelegate,ImagePickerBtnDelegate,QBImagePickerControllerDelegate,PickerViewCellDelegate,ColorPikerDelegate,ListPickerDelegate,PostSUSViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,EidtTableViewCellDelegate,UITableViewDataSource,UITableViewDelegate,RadioButtonClickDelegate>
@property (nonatomic,strong) selectView *selectview;
@property (nonatomic,strong) UIImagePickerController *picker;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) NSMutableArray *setetionarray;
@property (nonatomic,strong) PickViewPhotoTableViewCell *currtcell;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong)  Pubmode *leixing;
@property (nonatomic,assign)  NSInteger currpostion;
@property (nonatomic,copy)  NSString *csid;
@property (nonatomic,strong) CarpubMode *youhui;
@property (nonatomic,strong) Pubmode *baojia;
@property (nonatomic,strong) Pubmode *peizhi;
@property (nonatomic,strong) Pubmode *waiguan;
@property (nonatomic,strong) Pubmode *neishi;
@property (nonatomic,strong) CarPickModel *chexing;
@property (nonatomic,strong) Pubmode *kuwei;
@property (nonatomic,strong) Pubmode *quyu;
@property (nonatomic,strong) Pubmode *shouxu;
@property (nonatomic,strong) Pubmode *peizhijia;
@property (nonatomic,strong) Pubmode *yikoujia;
@property (nonatomic,strong) Pubmode *xingzhi;
@property (nonatomic,strong) Pubmode *paifang;
@property (nonatomic,strong) Pubmode *daiqi;
@property (nonatomic,strong) Pubmode *chejiahao;
@property (nonatomic,strong) CarpubMode *carimg;
@property (nonatomic,strong) CarpubMode *carinofimg;
@property (nonatomic,strong) NSArray *kuweiarray;
@property (nonatomic,strong) NSArray *quyuarray;
@property (nonatomic,strong) NSArray *shouxuarray;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) CarDetailmodel *cardetailmode;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) IQKeyboardReturnKeyHandler *returnKeyHandler;
@end

@implementation PostCarTableViewController

-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
-(void)radioclickwithcell:(RadioTableViewCell *)cell withIndex:(NSInteger)index{
    if (cell.mode==self.yikoujia) {
        if (index==2) {
            if ([self.setetionarray containsObject:self.baojia]) {
                [self.setetionarray removeObject:self.baojia];
            }
            if ([self.setetionarray containsObject:self.youhui]) {
                [self.setetionarray removeObject:self.youhui];
            }
            
            NSIndexSet *set=[NSIndexSet indexSetWithIndex:0];
            
            [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
        }else{
            
            if (![self.setetionarray containsObject:self.youhui]&&[self.leixing.detail isEqualToString:@"中规"]) {
                [self.setetionarray addObject:self.youhui];
                
            }
            
            if (![self.setetionarray containsObject:self.baojia]) {
                [self.setetionarray addObject:self.baojia];
            }
            NSIndexSet *set=[NSIndexSet indexSetWithIndex:0];
            
            [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
}
+(instancetype)viewcontroller{
    PostCarTableViewController *pvc=[[PostCarTableViewController alloc]init];
    pvc.hidesBottomBarWhenPushed=YES;
    return pvc;
}
-(UITableView *)tableView{
    if (_tableView==nil) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
-(void)photoCellView:(PickViewPhotoTableViewCell *)cell pickerBtnClick:(NSInteger)index withButton:(UIButton *)btn{
    self.button=btn;
    self.currtcell=cell;
    
    if (index==0) {
        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
        self.picker.sourceType=sourcheType;
        [self presentViewController:self.picker animated:YES completion:nil];
    }else{
        
        
        NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        
        
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        if (btn.isSelected) {
            imagePickerController.maximumNumberOfSelection = 1;
        }else{
            imagePickerController.maximumNumberOfSelection = 8-photomode.imgarray.count;
        }
        imagePickerController.showsNumberOfSelectedAssets = YES;
        
        
        [self presentViewController:imagePickerController animated:YES completion:NULL];
    }
}

-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
        _picker.view.backgroundColor = [UIColor whiteColor];
        
        _picker.delegate =self;
        _picker.allowsEditing = YES;
    }
    return _picker;
}

-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    NSMutableArray *marray=[NSMutableArray array];
    
    [self dismissViewControllerAnimated:YES completion:^{
        for (ALAsset *asset in assets) {
            UIImage *image=[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
            [marray addObject:image];
        }
        NSIndexPath *indexpath=[self.tableView indexPathForCell:self.currtcell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        if (self.button.isSelected) {
            [photomode.imgarray replaceObjectAtIndex:self.button.tag withObject:marray[0]];
        }else{
            
            [photomode.imgarray addObjectsFromArray:marray];
        }
        [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
}
-(void)photoCellViewReloadImage:(PickViewPhotoTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    [self dismissViewControllerAnimated:YES completion:NULL];
}
-(NSMutableArray *)array{
    if (_array==nil) {
        _array=[NSMutableArray array];
    }
    return _array;
}
-(NSMutableArray *)setetionarray{
    if (_setetionarray==nil) {
        _setetionarray=[NSMutableArray array];
    }
    return _setetionarray;
}

#pragma -mark ViewDidload
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    self.returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
    self.returnKeyHandler.lastTextFieldReturnKeyType = UIReturnKeyDone;
    self.returnKeyHandler.toolbarManageBehaviour = IQAutoToolbarBySubviews;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.backgroundColor=bgGarycolor;
    self.navigationItem.title=self.ID?@"修改车源信息":@"发布车源";
    UINib *top=[UINib nibWithNibName:NSStringFromClass([PublishTopTableViewCell class]) bundle:nil];
    UINib *radio=[UINib nibWithNibName:NSStringFromClass([RadioTableViewCell class]) bundle:nil];
    UINib *edit=[UINib nibWithNibName:NSStringFromClass([EidtTableViewCell class]) bundle:nil];
    UINib *img=[UINib nibWithNibName:NSStringFromClass([PikerImageTableViewCell class]) bundle:nil];
    [self.tableView registerNib:top  forCellReuseIdentifier:@"top"];
    [self.tableView registerNib:edit  forCellReuseIdentifier:@"editcell"];
    [self.tableView registerNib:radio  forCellReuseIdentifier:@"radio"];
    [self.tableView registerNib:img  forCellReuseIdentifier:@"img"];
    UINib *picker=[UINib nibWithNibName:NSStringFromClass([PickerTableViewCell class]) bundle:nil];
    [self.tableView registerNib:picker forCellReuseIdentifier:@"picker"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getcarname:) name:NoticePostGetCar object:nil];
    [self stuppostbtn];
    if (self.ID) {
        [self getCarDetailMode];
    }else{
        [self setupdata];
    }
}


-(void)textFieldChange:(EidtTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    if (indexpath.section==0) {
        Pubmode *mode=self.setetionarray[indexpath.row];
        
        if (mode==self.peizhijia) {
            NSIndexPath *indexpath2=[NSIndexPath indexPathForRow:indexpath.row+1 inSection:0];
            PickerTableViewCell *cell2=[self.tableView cellForRowAtIndexPath:indexpath2];
            [self datachange:cell2];
        }
    }
    
    
}

-(void)textFieldBeginEidtChange:(EidtTableViewCell *)cell withTextFiled:(UITextField *)textfield{
    
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
#pragma -mark 颜色选择回调
-(void)ColorPikcerClickContoller:(ColorPickViewController *)picker withIndex:(NSInteger)index ColorStr:(NSString *)colorstr{
    NSIndexPath *indexpath;
    if(picker.view.tag==0){
        self.waiguan.detail=colorstr;
        self.waiguan.index=index;
        indexpath=[NSIndexPath indexPathForRow:2 inSection:0];
        
    }else{
        self.neishi.detail=colorstr;
        self.neishi.index=index;
        indexpath=[NSIndexPath indexPathForRow:3 inSection:0];
    }
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma -mark 车型选择回调
-(void)getcarname:(NSNotification *)notice{
    CarDetail *detail=notice.userInfo[@"cardetail"];
    CarbrandModel *brand=notice.userInfo[@"brand"];
    CarprogChildmode *carprogchild=notice.userInfo[@"cartory"];
    self.chexing.detail=detail.name;
    self.chexing.cardetail=detail;
    self.chexing.carporgchild=carprogchild;
    self.chexing.brand=brand;
    self.peizhijia.detail=nil;
    if ([self.leixing.detail isEqualToString:@"中规"]) {
        self.baojia.detail=self.chexing.cardetail.price;
    }else{
        self.baojia.detail=nil;
    }
    self.youhui.type=YOUHUIPOINT;
    self.youhui.detail=nil;
    if ([self.chexing.cardetail.is_conf integerValue]==1) {
        
        if ([self.setetionarray containsObject:self.peizhijia]) {
            
        }else{
            
            [self.setetionarray insertObject:self.peizhijia atIndex:4];
        }
    }else{
        [self.setetionarray removeObject:self.peizhijia];
    }
    NSIndexSet *set=[NSIndexSet indexSetWithIndex:0];
    
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
    
    
}
-(void)getCarDetailMode{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"where";
    params[@"is_sale"]=@"1";
    params[@"cond"]=[NSString stringWithFormat:@"id=%@",_ID];
    int power=[self.user.power intValue];
    if (power<5) {
        params[@"self_id"]=self.user.user_id;
        params[@"type"]=@"0";
    }else{
        params[@"self_id"]=self.user.com_id;
        if (power ==8) {
            params[@"type"]=@"2";
        }else{
            params[@"type"]=@"1";
        }
    }
    
    [PostUitls Post:carurl parameters:params success:^(id response) {
        NSDictionary *dict=response;
        IndexCarModel *resmode=[IndexCarModel indexcarWithDcit:dict];
        self.cardetailmode=resmode.data[0];
        [self setupdata];
        [self.tableView reloadData];
        NSLog(@"%@",response);
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

-(void)clearother{
    self.chexing.cardetail=nil;
    self.chexing.brand=nil;
    self.chexing.carporgchild=nil;
    self.chexing.detail=@"请选择";
    
    self.neishi.detail=@"请选择";
    self.waiguan.detail=@"请选择";
    self.yikoujia.radioindex=-1;
    self.xingzhi.radioindex=-1;
    self.paifang.radioindex=-1;
    self.daiqi.radioindex=-1;
    self.chejiahao.detail=nil;
    self.kuwei.detail=@"请选择";
    self.quyu.detail=@"请选择";
    self.shouxu.detail=@"请选择";
    self.peizhi.detail=@"请输入";
    [self.carimg.imgarray removeAllObjects];
    [self.carinofimg.imgarray removeAllObjects];
    
}

-(void)clickWithCellTag:(PickerTableViewCell *)cell index:(NSInteger)index{
    self.youhui.type=index;
}

#pragma -mark设置数据模型
-(void)setupdata{
    
    self.currpostion=-1;
    Pubmode *leixing=[[Pubmode alloc]init];
    leixing.name=@"车源类型";
    leixing.isMust=YES;
    
    if (self.cardetailmode) {
        leixing.detail=self.cardetailmode.car_source_type;
        CarSourceType *type= [CarSourceType getcarsoucebyText:leixing.detail];
        NSArray *array= [CarSourceType getCarSourceType];
        self.currpostion=[array indexOfObject:type];
        self.csid=type.csid;
        
    }else{
        leixing.detail=@"请选择";
    }
    leixing.block=^{
        CarsrouceTypePickerController *picker=[CarsrouceTypePickerController picker];
        picker.currtpostion=self.currpostion;
        picker.navtitle=leixing.name;
        picker.block=^(NSString *str,NSString *cs_id,NSInteger currtpostion){
            
            NSLog(@"-----begin");
            if (![self.leixing.detail isEqualToString:str]) {
                self.leixing.detail=str;
                self.currpostion=currtpostion;
                self.leixing.csid=cs_id;
                [self clearother];
                if ([str isEqualToString:@"中规"]) {
                    if (![self.setetionarray containsObject:self.youhui]) {
                        
                        if ([self.setetionarray containsObject:self.baojia]) {
                            NSInteger index=[self.setetionarray indexOfObject:self.baojia];
                            [self.setetionarray insertObject:self.youhui atIndex:index];
                        }else{
                            [self.setetionarray addObject:self.youhui];
                        }
                        
                    }
                    self.baojia.placehoder=@"自动生成";
                    self.baojia.isAluto=YES;
                    self.baojia.yuancolor=tintblue;
                }else{
                    
                    [self.setetionarray removeObject:self.youhui];
                    self.baojia.isAluto=NO;
                    self.baojia.yuancolor=textnormalGary;
                    self.baojia.placehoder=@"请输入";
                }
                
                self.youhui.type=YOUHUIPOINT;
                self.youhui.detail=nil;
                self.baojia.detail=nil;
                
                [self.tableView reloadData];
                 NSLog(@"-----end");
            }
        };
        
        [self.navigationController pushViewController:picker animated:YES];
        
    };
    self.leixing=leixing;
    CarPickModel *chexing=[[CarPickModel alloc]init];
    chexing.name=@"车型";
    chexing.isMust=YES;
    if (self.cardetailmode) {
        chexing.detail=self.cardetailmode.car_name;
        CarDetail *cardetail=[[CarDetail alloc]init];
        cardetail.ID=self.cardetailmode.mid;
        cardetail.name=self.cardetailmode.car_name;
        cardetail.price=self.cardetailmode.sprice;
        cardetail.is_conf=![self.cardetailmode.conf_price isEqualToString:@""]?@"0":@"1";
        chexing.cardetail=cardetail;
        
        CarbrandModel *brand=[[CarbrandModel alloc]init];
        brand.brand_id=self.cardetailmode.b_id;
        brand.brand_name=self.cardetailmode.brand_name;
        CarprogChildmode *child=[[CarprogChildmode alloc]init];
        child.cat_id=self.cardetailmode.cat_id;
        child.cat_name=self.cardetailmode.cat_name;
        chexing.carporgchild=child;
        chexing.brand=brand;
        
    }else{
        chexing.detail=@"请选择";
    }
    chexing.block=^{
        if ([self.leixing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车源类型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            FindCarTableViewController *fincar=[FindCarTableViewController findcar];
            fincar.type=self.leixing.csid;
            [self.navigationController pushViewController:fincar animated:YES];
        }
    };
    self.chexing=chexing;
    Pubmode *waiguan=[[Pubmode alloc]init];
    waiguan.name=@"外观颜色";
    waiguan.isMust=YES;
    waiguan.detail=self.cardetailmode.car_outside_color?self.cardetailmode.car_outside_color:@"请选择";
    waiguan.block=^{
        
        if ([self.chexing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            ColorPickViewController *color=[ColorPickViewController colorpicker];
            color.delegate=self;
            
            color.ID=self.chexing.cardetail.ID;
            color.index=self.waiguan.index;
            color.colorstr=self.waiguan.detail;
            color.isOut=YES;
            color.view.tag=0;
            [self.navigationController pushViewController:color animated:YES];}
    };
    self.waiguan=waiguan;
    Pubmode *neishi=[[Pubmode alloc]init];
    neishi.name=@"内饰颜色";
    neishi.isMust=YES;
    neishi.detail=self.cardetailmode.car_inside_color?self.cardetailmode.car_inside_color:@"请选择";;
    self.neishi=neishi;
    neishi.block=^{
        
        if ([self.chexing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            ColorPickViewController *color=[ColorPickViewController colorpicker];
            color.delegate=self;
            
            color.ID=self.chexing.cardetail.ID;
            color.index=self.neishi.index;
            color.colorstr=self.waiguan.detail;
            color.isOut=NO;
            color.view.tag=1;            [self.navigationController pushViewController:color animated:YES];}
        
    };
    Pubmode *yikoujia=[[Pubmode alloc]init];
    yikoujia.name=@"是否一口价";
    yikoujia.isMust=YES;
    yikoujia.isNotCancel=YES;
    yikoujia.titlearray=@[@"一口价",@"可议价",@"不报价"];
    yikoujia.radioindex=-1;
    if (self.cardetailmode.is_price) {
        yikoujia.radioindex=[self.cardetailmode.is_price integerValue];
    }
    
    
    self.yikoujia=yikoujia;
    Pubmode *baojia=[[Pubmode alloc]init];
    baojia.name=@"报价";
    baojia.isMust=YES;
    baojia.isEidt=YES;
    if (self.cardetailmode) {
        baojia.detail=self.cardetailmode.sysprice;
    }
    baojia.pricetitle=@"万";
    self.baojia=baojia;
    CarpubMode *youhui=[[CarpubMode alloc]init];
    youhui.name=@"优惠";
    youhui.isSelect=YES;
    
    if ([self.cardetailmode.car_source_type isEqualToString:@"中规"]) {
        if ([self.cardetailmode.discount_scale floatValue]!=0) {
            youhui.type=YOUHUIPOINT;
            youhui.detail=self.cardetailmode.discount_scale;
        }else if([self.cardetailmode.discount floatValue]!=0){
            youhui.type=YOUHUIPRICE;
            youhui.detail=self.cardetailmode.discount_scale;
        }else if([self.cardetailmode.sprice floatValue]<[self.cardetailmode.sysprice floatValue]){
            youhui.type=PLUSPOINT;
            float price=[self.cardetailmode.sysprice floatValue]-[self.cardetailmode.sprice floatValue];
            youhui.detail=[NSString stringWithFormat:@"%.2f",price];
        }else{
            youhui.type=DIRESTPRICE;
            youhui.detail=self.cardetailmode.sysprice;
        }
        
    }else{
        youhui.type=YOUHUIPOINT;
    }
    self.youhui=youhui;
    Pubmode *peizhijia=[[Pubmode alloc]init];
    peizhijia.name=@"配置价";
    peizhijia.isMust=YES;
    peizhijia.isEidt=YES;
    if (self.cardetailmode.conf_price) {
        peizhijia.detail=self.cardetailmode.conf_price;
    }
    peizhijia.pricetitle=@"万";
    self.peizhijia=peizhijia;
    
    
    [self.setetionarray addObject:leixing];
    [self.setetionarray addObject:chexing];
    [self.setetionarray addObject:waiguan];
    [self.setetionarray addObject:neishi];
    if ([self.cardetailmode.conf_price floatValue]!=0&&self.cardetailmode.conf_price) {
        [self.setetionarray addObject:peizhijia];
    }
    [self.setetionarray addObject:yikoujia];
    
    if ([self.cardetailmode.car_source_type isEqualToString:@"中规"]) {
        [self.setetionarray addObject:youhui];
    }
    if ([self.cardetailmode.is_price integerValue]!=2) {
        [self.setetionarray addObject:baojia];
    }
    
    
    
    
    
    Pubmode *cheyuanxingzhi=[[Pubmode alloc]init];
    cheyuanxingzhi.name=@"车源性质";
    cheyuanxingzhi.isMust=NO;
    cheyuanxingzhi.titlearray=@[@"资源",@"自店"];
    cheyuanxingzhi.radioindex=-1;
    self.xingzhi=cheyuanxingzhi;
    if (self.cardetailmode) {
        switch ([self.cardetailmode.car_nature integerValue]) {
            case 0:
                cheyuanxingzhi.radioindex=0;
                break;
            case 1:
                cheyuanxingzhi.radioindex=1;
                break;
                
            default:
                cheyuanxingzhi.radioindex=-1;
                break;
        }
        
        
    }
    
    Pubmode *paifang=[[Pubmode alloc]init];
    paifang.name=@"排放标准";
    paifang.isMust=NO;
    paifang.titlearray=@[@"国四",@"国五",@"不限"];
    paifang.radioindex=-1;
    if (self.cardetailmode) {
        switch ([self.cardetailmode.standard integerValue]) {
            case 4:
                paifang.radioindex=0;
                break;
            case 5:
                paifang.radioindex=1;
                break;
            case 0:
                paifang.radioindex=2;
                break;
            default:
                paifang.radioindex=-1;
                break;
        }
        
    }
    self.paifang=paifang;
    Pubmode *qidai=[[Pubmode alloc]init];
    qidai.name=@"货期";
    qidai.isMust=NO;
    qidai.titlearray=@[@"期货",@"现货"];
    qidai.radioindex=-1;
    if (self.cardetailmode) {
        switch ([self.cardetailmode.timer integerValue]) {
            case 0:
                qidai.radioindex=0;
                break;
            case 1:
                qidai.radioindex=1;
                break;
                
            default:
                qidai.radioindex=-1;
                break;
        }
      
    }
    self.daiqi=qidai;
    Pubmode *chejiahao=[[Pubmode alloc]init];
    chejiahao.name=@"车架号";
    chejiahao.isEidt=YES;
    chejiahao.placehoder=@"请输入车架号";
    chejiahao.detail=self.cardetailmode.car_number;
    chejiahao.keytype=ASCIIPAD;
    self.chejiahao=chejiahao;
    Pubmode *kuwei=[[Pubmode alloc]init];
    kuwei.name=@"库位";
    kuwei.detail=self.cardetailmode.stock_position==nil||[self.cardetailmode.stock_position isEqualToString:@""]?@"请选择":self.cardetailmode.stock_position;
    kuwei.block=^{
        [self showpicker:0];
    };
    
    self.kuwei=kuwei;
    
    Pubmode *quyu=[[Pubmode alloc]init];
    quyu.name=@"销售区域";
    quyu.detail=self.cardetailmode.sale_area==nil||[self.cardetailmode.sale_area isEqualToString:@""]?@"请选择":self.cardetailmode.sale_area;
    quyu.block=^{
        [self showpicker:1];
    };
    self.quyu=quyu;
    Pubmode *shouxu=[[Pubmode alloc]init];
    shouxu.name=@"手续";
    shouxu.detail=self.cardetailmode.sale_area==nil||[self.cardetailmode.sale_area isEqualToString:@""]?@"请选择":self.cardetailmode.fee;
    shouxu.block=^{
        [self showpicker:2];
    };
    self.shouxu=shouxu;
    NSArray *array=@[cheyuanxingzhi,paifang,qidai,chejiahao,kuwei,quyu,shouxu];
    
    Pubmode *peizhi=[[Pubmode alloc]init];
    peizhi.name=@"配置";
    peizhi.detail=self.cardetailmode.car_configure==nil||[self.cardetailmode.car_configure isEqualToString:@""]?@"请输入":self.cardetailmode.car_configure;
    self.peizhi=peizhi;
    peizhi.block=^{
        JianjieViewController *jjvc=[JianjieViewController viewcontroller];
        jjvc.navtitle=@"配置";
        jjvc.textlength=500;
        jjvc.text=[self.peizhi.detail isEqualToString:@"请输入"]?@"":self.peizhi.detail;
        jjvc.block=^(NSString *str){
            if ([str isEqualToString:@""]||str==nil) {
                str=@"请输入";
            }
            self.peizhi.detail=str;
            NSIndexPath *indexpath=[NSIndexPath indexPathForRow:0 inSection:2];
            [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
            
        };
        [self.navigationController pushViewController:jjvc animated:YES];
    };
    CarpubMode *carmode=[[CarpubMode alloc]init];
    carmode.name=@"车辆图片";
    carmode.isPhotoCell=YES;
    carmode.imgarray=[NSMutableArray array];
    if (self.cardetailmode.imgs.count>0) {
        carmode.isHasURL=YES;
    }
    carmode.urlarray=self.cardetailmode.imgs;
    self.carimg=carmode;
    CarpubMode *carmode2=[[CarpubMode alloc]init];
    carmode2.name=@"车辆资料";
    carmode2.isPhotoCell=YES;
    if (self.cardetailmode.vehicle_imgs.count>0) {
        carmode2.isHasURL=YES;
    }
    carmode2.imgarray=[NSMutableArray array];
    carmode2.urlarray=self.cardetailmode.vehicle_imgs;
    self.carinofimg=carmode2;
    [self.array addObject:self.setetionarray];
    [self.array addObject:array];
    [self.array addObject:@[peizhi]];
    [self.array addObject:@[carmode,carmode2]];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
    
    
}

#pragma -mark 初始化发送按钮
/**
 *  初始化发送按钮
 */
-(void)stuppostbtn{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 54)];
    view.backgroundColor=[UIColor clearColor];
    UIButton *postbtn=[[UIButton alloc]initWithFrame:CGRectMake(16, 8, XHScreenwidth-32, 36)];
    [postbtn addTarget:self action:@selector(postclick) forControlEvents:UIControlEventTouchUpInside];
    [postbtn setBackgroundImage:[UIImage imageWithStretchableName:@"button_f"] forState:UIControlStateNormal];
    [postbtn setTitle:@"立即发布" forState:UIControlStateNormal];
    [postbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [view addSubview:postbtn];
    self.tableView.tableFooterView=view;
}

/**
 *  发送点击事件
 */

-(void)continueClick{
    self.ID=nil;
    self.navigationItem.title=@"发布车源";
    [self.setetionarray removeAllObjects];
    [self.array removeAllObjects];
    [self setupdata];
    [self.tableView reloadData];
}

-(void)postclick{
    if ([self checkfrom]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=self.ID?@"正在修改...":@"正在发布...";
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if (self.ID) {
            params[@"action"]=@"car_edit";
            params[@"car_source_type"]=self.leixing.detail;
        }else{
            params[@"action"]=@"add";
            params[@"type_word"]=self.leixing.detail;
        }
        NSInteger power=[self.user.power integerValue];
        if (power==1) {
            params[@"user_id"]=self.user.user_id;
            if (self.ID) {
                params[@"type"]=@"person";
            }
            
        }else if (power==5){
            if (self.ID) {
                params[@"user_id"]=self.user.com_id;
                params[@"type"]=@"company";
            }else{
                params[@"com_id"]=self.user.com_id;
            }
        }else if (power==8){
            if (self.ID) {
                params[@"user_id"]=self.user.com_id;
                params[@"type"]=@"company";
            }else{
                params[@"com_id"]=self.user.com_id;
                params[@"parent_id"]=self.user.parent_id;
            }
        }
        
        if (self.cardetailmode) {
            params[@"car_id"]=self.cardetailmode.ID;
        }
        CarSourceType *type=[CarSourceType getcarsoucebyText:self.leixing.detail];
        params[@"type_id"]=type.csid;
        
        if ([type.csid integerValue]==5&&self.yikoujia.radioindex!=2) {
            switch (self.youhui.type) {
                case 0:
                    if (self.ID) {
                        params[@"discount_scale"]=self.youhui.detail;
                    }else{
                        params[@"disc"]=self.youhui.detail;
                    }
                    break;
                case 1:
                    if (self.ID) {
                        params[@"discount"]=self.youhui.detail;
                    }else{
                        params[@"disp"]=self.youhui.detail;
                    }
                    break;
                case 2:
                    if (self.ID) {
                        params[@"addprice"]=self.youhui.detail;
                    }else{
                        params[@"addp"]=self.youhui.detail;
                    }
                    break;
                case 3:
                    if (self.ID) {
                        
                    }else{
                        params[@"just_price"]=self.baojia.detail;
                    }
                    break;
                default:
                    break;
            }
        }
        if (self.ID) {
            params[@"brand_name"]=self.chexing.brand.brand_name;
            params[@"mid"]=self.chexing.cardetail.ID;
            params[@"car_outside_color"]=self.waiguan.detail;
            params[@"car_inside_color"]=self.neishi.detail;
            
            if (self.yikoujia.radioindex!=2) {
                params[@"sysprice"]=self.baojia.detail;
            }
            
            
            params[@"is_price"]=[NSString stringWithFormat:@"%ld",self.yikoujia.radioindex];
        }else{
            params[@"b_name"]=self.chexing.brand.brand_name;
            params[@"car_type"]=self.chexing.cardetail.ID;
            params[@"out_c"]=self.waiguan.detail;
            params[@"in_c"]=self.neishi.detail;
            if (self.yikoujia.radioindex!=2) {
                params[@"price"]=self.baojia.detail;
            }
            
            params[@"one_price"]=[NSString stringWithFormat:@"%ld",self.yikoujia.radioindex];
            
        }
        params[@"cat_name"]=self.chexing.carporgchild.cat_name;
        params[@"cat_id"]=self.chexing.carporgchild.cat_id;
        
        params[@"car_name"]=self.chexing.cardetail.name;
        
        if (self.paifang.radioindex!=-1) {
            NSString *st=@"";
            switch (self.paifang.radioindex) {
                case 0:
                    st=@"4";
                    break;
                case 1:
                    st=@"5";
                    break;
                case 2:
                    st=@"0";
                    break;
                default:
                    break;
            }
            if (self.ID) {
                params[@"standard"]=st;
            }else{
                params[@"s_tand"]=st;
            }
        }
        if (self.daiqi.radioindex!=-1) {
            if (self.ID) {
                params[@"timer"]=[NSString stringWithFormat:@"%ld",self.daiqi.radioindex];
            }else{
                params[@"tner"]=[NSString stringWithFormat:@"%ld",self.daiqi.radioindex];
            }
            
        }
        if (self.xingzhi.radioindex!=-1) {
            
            if (self.ID) {
                params[@"car_nature"]=[NSString stringWithFormat:@"%ld",self.xingzhi.radioindex];
            }else{
                params[@"nature"]=[NSString stringWithFormat:@"%ld",self.xingzhi.radioindex];
            }
        }
        
        if (self.chejiahao.detail&&![self.chejiahao.detail isEqualToString:@""]) {
            
            if (self.ID) {
                params[@"car_number"]=self.chejiahao.detail;
            }else{
                params[@"car_nu"]=self.chejiahao.detail;
            }
            
        }
        
        if (self.peizhijia.detail!=nil) {
            if (self.ID) {
                params[@"conf_price"]=self.peizhijia.detail;
            }else{
                params[@"conf"]=self.peizhijia.detail;
            }
        }else{
            if (self.ID) {
                params[@"conf_price"]=@"0";
            }
        }
        
        if (self.kuwei.detail&&![self.kuwei.detail isEqualToString:@"请选择"]) {
            if (self.ID) {
                params[@"stock_position"]=self.kuwei.detail;
            }else{
                params[@"s_potion"]=self.kuwei.detail;}
        }
        if (self.quyu.detail&&![self.quyu.detail isEqualToString:@"请选择"]) {
            if (self.ID) {
                params[@"sale_area"]=self.quyu.detail;
            }else{
                params[@"s_area"]=self.quyu.detail;}
        }
        if (self.shouxu.detail&&![self.shouxu.detail isEqualToString:@"请选择"]) {
            params[@"fee"]=self.shouxu.detail;
        }
        
        if (self.peizhi.detail&&![self.peizhi.detail isEqualToString:@"请输入"]) {
            if (self.ID) {
                params[@"car_configure"]=self.peizhi.detail;
            }else{
                params[@"configure"]=self.peizhi.detail;}
        }
        
        if (self.carimg.imgarray.count>0) {
            
            NSMutableArray *imgstrarray=[NSMutableArray array];
            
            for (UIImage *image in self.carimg.imgarray) {
                NSData *imagedata=[UIImage compressData:image toMaxFileSize:DefailutPicSize];
                NSString *base64str=[imagedata imageBase64Data:imagedata];
                [imgstrarray addObject:base64str];
            }
            NSString *imgp=[NSString DataTOjsonString:imgstrarray];
            params[@"imgs"]=imgp;
        }
        if (self.carinofimg.imgarray.count>0) {
            
            NSMutableArray *imgstrarray=[NSMutableArray array];
            
            for (UIImage *image in self.carinofimg.imgarray) {
                NSData *imagedata=[UIImage compressData:image toMaxFileSize:DefailutPicSize];
                NSString *base64str=[imagedata imageBase64Data:imagedata];
                [imgstrarray addObject:base64str];
            }
            NSString *imgp=[NSString DataTOjsonString:imgstrarray];
            
//            NSLog(@"%@",imgp);
            
            if (self.ID) {
                params[@"car_imgs"]=imgp;
            }else{
                params[@"imginfo"]=imgp;
            }
        }
        NSString *url;
        if (self.ID) {
            url=mycarsource;
        }else{
            url=addcarurl;
        }
        
        [PostUitls Post:url parameters:params success:^(id response) {
            NSLog(@"%@",response);
            
            hud.mode=MBProgressHUDModeText;
            
            
            PostResdemode *resd =[PostResdemode objectWithKeyValues:response];
            
            if ([resd.state integerValue]==400) {
                PostSUSViewController *svc=[PostSUSViewController viewcontroller];
                svc.delegate=self;
                svc.type=POSTCARTYPE;
                if (self.ID) {
                    svc.ID=self.ID;
                }else{
                    svc.ID=[NSString stringWithFormat:@"%ld",[resd.car_id integerValue]];
                }
                [self.navigationController pushViewController:svc animated:YES];
            }else{
                hud.labelText=resd.msg;
            }
            [hud hide:YES];
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            hud.labelText=@"网络错误";
            hud.mode=MBProgressHUDModeText;
            [hud hide:YES afterDelay:0.5];
        }];
    }
}
/**
 *  检查提交的数据
 *
 *  @return YES or NO
 */
-(BOOL)checkfrom{
    if ([self.leixing.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择车源类型";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.chexing.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择车型";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.waiguan.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择外观颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.neishi.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择内饰颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if ([self.neishi.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择内饰颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if ([self.leixing.detail isEqualToString:@"中规"]&&[self.chexing.cardetail.is_conf integerValue]==1&&self.peizhijia.detail==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入配置价";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if ([self.leixing.detail isEqualToString:@"中规"]&&self.youhui.detail==nil&&self.yikoujia.radioindex!=2) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        switch (self.youhui.type) {
            case 0:
                hud.labelText=@"请输入优惠点数";
                break;
            case 1:
                hud.labelText=@"请输入优惠万元";
                break;
            case 2:
                hud.labelText=@"请输入加价万元";
                break;
            case 3:
                hud.labelText=@"请输入直接报价";
                break;
            default:
                break;
        }
        
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if (self.baojia.detail==nil&&self.yikoujia.radioindex!=2) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入报价";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if (self.yikoujia.radioindex==-1) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择是否一口价";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ((self.peizhijia.detail==nil&&[self.chexing.cardetail.is_conf integerValue]==1)||[self.peizhijia.detail isEqualToString:@""]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入配置价";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    return YES;
}

-(NSArray *)kuweiarray{
    if (_kuweiarray==nil) {
        NSMutableArray *marray=[NSMutableArray array];
        NSArray *array=[Position getPostion];
        for (Position *postion in array) {
            [marray addObject:postion.name];
        }
        _kuweiarray=marray;
    }
    return _kuweiarray;
}
-(NSArray *)quyuarray{
    if (_quyuarray==nil) {
        NSMutableArray *marray=[NSMutableArray array];
        NSArray *array=[Area getArea];
        for (Area *ar in array) {
            [marray addObject:ar.name];
        }
        _quyuarray=marray;
    }
    return _quyuarray;
}
-(NSArray *)shouxuarray{
    if (_shouxuarray==nil) {
        NSMutableArray *marray=[NSMutableArray array];
        NSArray *array=[Process getProcess];
        for (Process *pr in array) {
            [marray addObject:pr.p_name];
        }
        _shouxuarray=marray;
    }
    return _shouxuarray;
}


#pragma -mark 选择控制器的回调方法

-(void)ListPickerDelegate:(ListPikerController *)picker WithIndex:(NSInteger)index{
    
    switch (picker.view.tag) {
        case 0:
        {
            self.kuwei.detail=self.kuweiarray[index];
            self.kuwei.index=index;
            
        }
            break;
        case 1:
        {
            self.quyu.detail=self.quyuarray[index];
            self.quyu.index=index;
            
        }
            break;
        case 2:
        {
            self.shouxu.detail=self.shouxuarray[index];
            self.shouxu.index=index;
        }
            break;
            
        default:
            break;
    }
    NSIndexSet *sets=[NSIndexSet indexSetWithIndex:1];
    [self.tableView reloadSections:sets withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)ListPickerDelegate:(ListPikerController *)picker WithCumerString:(NSString *)string{
    
    if (picker.view.tag==0) {
        
        self.kuwei.detail=string;
    }else if (picker.view.tag==1){
        self.quyu.detail=string;
        
        
    }
    NSIndexSet *sets=[NSIndexSet indexSetWithIndex:1];
    [self.tableView reloadSections:sets withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma -mark 显示选择控制器
-(void)showpicker:(NSInteger)tag{
    ListPikerController *picker=[ListPikerController viewcontroller];
    picker.view.tag=tag;
    picker.delegate=self;
    switch (tag) {
        case 0:
            picker.currtpostion=self.kuwei.index;
            picker.dataarray=self.kuweiarray;
            picker.navigationItem.title=@"库位";
            picker.iscanSelect=YES;
            break;
        case 1:
            picker.currtpostion=self.quyu.index;
            picker.iscanEidt=YES;
            picker.dataarray=self.quyuarray;
            picker.navigationItem.title=@"销售区域";
            break;
        case 2:
            picker.currtpostion=self.shouxu.index;
            picker.dataarray=self.shouxuarray;
            picker.navigationItem.title=@"手续";
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:picker animated:YES];
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.tableView endEditing:YES];
}
-(void)datachange:(PickerTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    
    switch (self.youhui.type) {
        case 0:
        { float p =[self.youhui.detail floatValue];
            if (p>100||p<0) {
                MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
                hud.labelText=@"优惠点数只能在0-100之间";
                hud.mode = MBProgressHUDModeText;
                hud.margin = 10.f;
                hud.removeFromSuperViewOnHide = YES;
                [hud hide:YES afterDelay:1];
                self.youhui.detail=nil;
                cell.textinput.text=nil;
            }else{
                float price=self.peizhijia.detail==nil||[self.peizhijia.detail isEqualToString:@""]? [self.chexing.cardetail.price floatValue]:[self.peizhijia.detail floatValue];
                price=(1-p/100.0f)*price;
                self.baojia.detail=[NSString stringWithFormat:@"%.2f",price];
            }
            
            NSIndexPath *indexpath2=[NSIndexPath indexPathForRow:indexpath.row+1 inSection:0];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexpath2] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 1:
        { float p =[self.youhui.detail floatValue];
            float price=self.peizhijia.detail==nil||[self.peizhijia.detail isEqualToString:@""]? [self.chexing.cardetail.price floatValue]:[self.peizhijia.detail floatValue];
            price=price-p;
            if (price<0) {
                self.youhui.detail=nil;
                self.baojia.detail=nil;
                MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
                hud.labelText=@"优惠价格不能超过指导价或配置价";
                hud.mode = MBProgressHUDModeText;
                hud.margin = 10.f;
                hud.removeFromSuperViewOnHide = YES;
                [hud hide:YES afterDelay:1];
                cell.textinput.text=nil;
            }else{
                self.baojia.detail=[NSString stringWithFormat:@"%.2f",price];
            }
            NSIndexPath *indexpath2=[NSIndexPath indexPathForRow:indexpath.row+1 inSection:0];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexpath2] withRowAnimation:UITableViewRowAnimationAutomatic];
            
        }
            break;
        case 2:
        { float p =[self.youhui.detail floatValue];
            float price=self.peizhijia.detail==nil||[self.peizhijia.detail isEqualToString:@""]? [self.chexing.cardetail.price floatValue]:[self.peizhijia.detail floatValue];
            price=price+p;
            self.baojia.detail=[NSString stringWithFormat:@"%.2f",price];
            NSIndexPath *indexpath2=[NSIndexPath indexPathForRow:indexpath.row+1 inSection:0];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexpath2] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 3:
        { float p =[self.youhui.detail floatValue];
            self.baojia.detail=[NSString stringWithFormat:@"%.2f",p];
            NSIndexPath *indexpath2=[NSIndexPath indexPathForRow:indexpath.row+1 inSection:0];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexpath2] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSArray *array=self.array[section];
    return array.count;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.isSelect) {
        return 44;
    }else if ([mode isKindOfClass:[CarpubMode class]]) {
        CarpubMode *photo=mode;
        
        if (photo.imgarray.count>=4) {
            return 179;
        }else{
            return 111;
        }
    }else{
        return 44;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.titlearray) {
        RadioTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"radio"];
        cell.mode=mode;
        cell.ismustlable.hidden=!mode.isMust;
        cell.delegate=self;
        return cell;
    }else if (mode.isSelect){
        CarpubMode *cmode=mode;
        PickerTableViewCell *cell=[PickerTableViewCell pickercellWithTableview:tableView];
        cell.delegate=self;
        cell.mode=cmode;
        return cell;
    }
    else if (mode.isEidt){
        EidtTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"editcell"];
        cell.mode=mode;
        cell.price.text=mode.pricetitle;
        cell.delegate=self;
        if (mode.keytype ==0) {
            cell.detail.keyboardAppearance=UIKeyboardAppearanceLight;
            cell.detail.keyboardType=UIKeyboardTypeDecimalPad;
        }else{
            cell.detail.keyboardType=UIKeyboardTypeASCIICapable;
        }
        
        return cell;
    }else if ([mode isKindOfClass:[CarpubMode class]]){
        CarpubMode *photomode=mode;
        
        PickViewPhotoTableViewCell *picker=[PickViewPhotoTableViewCell cellWithTablView:tableView];
        picker.delegate=self;
        picker.mode=photomode;
        return picker;
    }
    else if ([mode isKindOfClass:[CarPickModel class]]&&[self.leixing.detail isEqualToString:@"中规"]){
        CarDetailTableViewCell *cell=[CarDetailTableViewCell cellwithTablview:tableView];
        if (self.chexing.cardetail) {
            cell.carname.text=self.chexing.cardetail.name;
            cell.carname.textColor=textnormalGary;
            cell.price.text=[NSString stringWithFormat:@"%@",self.chexing.cardetail.price];
            cell.price.textColor=textnormalGary;
        }else{
            cell.carname.text=nil;
            cell.price.text=@"请选择";
            cell.price.textColor=placehodlerColor;
            cell.carname.textColor=placehodlerColor;
        }
        
        return cell;
    }
    
    else
    {
        PublishTopTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"top"];
        cell.titlelable.text=mode.name;
        cell.startlable.hidden=!mode.isMust;
        cell.detail.text=mode.detail;
        if ([mode.detail isEqualToString:@"请选择"]||[mode.detail isEqualToString:@"请输入"]) {
            cell.detail.textColor=placehodlerColor;
        }else{
            cell.detail.textColor=textnormalGary;
        }
        return cell;
        
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        NSIndexPath *indexpath=[self.tableView indexPathForCell:self.currtcell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        if (self.button.isSelected) {
            [photomode.imgarray replaceObjectAtIndex:self.button.tag withObject:image];
        }else{
            [photomode.imgarray addObject:image];
        }
        
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
